package com.hhxy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhxy.model.dto.AddMenuDto;
import com.hhxy.model.entity.Menu;
import com.hhxy.model.vo.MenuTreeVo;
import com.hhxy.model.vo.MenuVo;
import com.hhxy.model.vo.RoleMenuTreeSelectVo;
import com.hhxy.service.MenuService;
import com.hhxy.utils.BeanCopyUtils;
import com.hhxy.utils.convert.MenuConverter;
import com.hhxy.utils.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author ghp
 * @date 2023/5/3
 * @title
 * @description
 */
@Api(tags = {"后端接口", "菜单管理", "系统管理"})
@ApiSupport(author = "ghp", order = 1)
@RestController
@RequestMapping("/system/menu")
@Validated
public class BeMenuController {

    @Resource
    private MenuService menuService;

    @GetMapping("/list")
    @ApiOperation(value = "分页条件查询菜单信息", notes = "pageNum和pageSize不能为空，条件为menuName和status")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuName", value = "菜单名成"),
            @ApiImplicitParam(name = "status", value = "菜单状态 0-正常 1-禁用")
    })
    public ResponseResult list(String menuName, String status) {
        return menuService.list(menuName, status);
    }

    @PostMapping
    @ApiOperation(value = "新增菜单")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult add(@RequestBody @Valid AddMenuDto addMenuDto) {
        return menuService.add(addMenuDto);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询菜单")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "菜单id")
    public ResponseResult getMenuById(@PathVariable @NotNull(message = "菜单id不能为空") Long id) {
        return ResponseResult.okResult(menuService.getById(id));
    }

    @PutMapping
    @ApiOperation(value = "更新菜单")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult updateMenuById(@RequestBody @Valid AddMenuDto addMenuDto) {
        return menuService.updateMenuById(addMenuDto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "修改菜单信息")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "菜单id")
    public ResponseResult deleteMenuById(@PathVariable @NotNull(message = "菜单id不能为空") Long id) {
        return menuService.deleteMenuById(id);
    }


    /**
     * 获取菜单下拉树列表（新增角色时需要用到）
     */
    @GetMapping("/treeselect")
    @ApiOperation(value = "查询角色的权限信息", tags = "角色管理")
    public ResponseResult treeselect() {
        // 查询出所有的权限信息
        ResponseResult result = menuService.list(null, null);
        List<MenuVo> menuVoList = (List<MenuVo>) result.getData();
        // 将查询到的结果构建成 tree 的形式
        List<Menu> menuList = BeanCopyUtils.copyBeanList(menuVoList, Menu.class);
        List<MenuTreeVo> options = MenuConverter.buildMenuSelectTree(menuList);
        return ResponseResult.okResult(options);
    }

    /**
     * 获取角色与菜单权限的关系（修改角色时需要用到）
     */
    @GetMapping(value = "/roleMenuTreeselect/{id}")
    public ResponseResult roleMenuTreeSelect(@PathVariable Long id) {
        // 获取所有的权限信息
        ResponseResult result = menuService.list(null, null);
        List<MenuVo> menuVoList = (List<MenuVo>) result.getData();
        List<Menu> menuList = BeanCopyUtils.copyBeanList(menuVoList, Menu.class);
        // 获取角色所对应的菜单id（前端根据这个判断勾选了哪些权限）
        List<Long> checkedKeys = menuService.selectMenuListByRoleId(id);
        // 将查询的的信息构建成Tree（前端根据这个展示可勾选的菜单列表）
        List<MenuTreeVo> menuTreeVos = MenuConverter.buildMenuSelectTree(menuList);
        RoleMenuTreeSelectVo vo = new RoleMenuTreeSelectVo(checkedKeys, menuTreeVos);
        return ResponseResult.okResult(vo);
    }


}
